﻿using Sample.BusinessComponent;
using Sample.BusinessEntities;
using Sample.Common.Mvc;
using Sample.Web.Areas.Sample.Models;
using Sample.Web.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Webdiyer.WebControls.Mvc;

namespace Sample.Web.Areas.Sample.Controllers
{
    [Authorize]
    public class SampleController : BaseController<PersonBC>
    {
        // GET: Sample
        public ActionResult Index(string name, DateTime? birthdate, int page = 1)
        {
            int totalCount;

            var people = this.Service.GetPersonsByCriteria(name, birthdate, out totalCount, page);

            var pagedPeople = people.ToPagedList(totalCount, page, Common.Constants.PageSize);

            var model = new PersonQueryModel();

            model.PersonModels = pagedPeople;

            if (Request.IsAjaxRequest())
                return PartialView("_PersonList", pagedPeople);
            return View(model);

       
        }
        
        // GET: Sample/Details/5
        public ActionResult Details(int id)
        {
            return View();
        }

        // GET: Sample/Create
        public ActionResult Create()
        {
            return View();
        }

        // POST: Sample/Create
        [HttpPost]
        public ActionResult Create(PersonModel personModel)
        {
            if (ModelState.IsValid)
            {
                this.Service.AddPerson(personModel);

                return RedirectToAction("Index");

            }
            //validation failed, return model
            return View(personModel);

        }

        // GET: Sample/Edit/5
        public ActionResult Edit(int id)
        {
            var model = this.Service.GetPerson(id);

            return View(model);
        }

        // POST: Sample/Edit/5
        [HttpPost]
        public ActionResult Edit(PersonModel personModel)
        {

            if (ModelState.IsValid)
            {
                this.Service.UpdatePerson(personModel);
                return RedirectToAction("Index");
            }
            return View(personModel);

        }

        // GET: Sample/Delete/5
        public ActionResult Delete(int id)
        {
            return View();
        }

        // POST: Sample/Delete/5
        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
